package com.finance.cooperate.feature.service.impl;

import com.finance.cooperate.common.utils.DateUtils;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.dao.entity.primary.Apply;
import com.finance.cooperate.dao.entity.primary.Borrowing;
import com.finance.cooperate.dao.local.scope.CooperateRequestScope;
import com.finance.cooperate.dao.repository.primary.ApplyRepository;
import com.finance.cooperate.dao.repository.primary.BorrowingRepository;
import com.finance.cooperate.feature.dto.ApplyDTO;
import com.finance.cooperate.feature.service.ApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @ClassName ApplyServiceImpl
 * @Description TODO
 * @Author shen
 * @Date 2023/8/6 22:26
 * @Modify ...
 */
@Service
@Slf4j
public class ApplyServiceImpl implements ApplyService {

    @Autowired
    private ApplyRepository applyRepository;

    @Autowired
    private CooperateRequestScope requestScope;

    @Autowired
    private BorrowingRepository borrowingRepository;



    @Override
    public Date getLastSubmitTime(String uid) {

        Date lastSubmitTime = requestScope.getLastSubmitTime();

        if (lastSubmitTime != null) {
            return lastSubmitTime;
        }

        String applyId = requestScope.getApplyId();

        if (!StringUtil.isEmptyOrNvl(applyId)) {

            Optional<Apply> optional = applyRepository.findById(applyId);

            if (optional.isPresent()) {
                Apply apply = optional.get();

                lastSubmitTime = DateUtils.addMinute(apply.getSubmitTime(), 30);

                // 临时缓存首贷进件时间：
                requestScope.setFirstLastSubmitTime(DateUtils.addMinute(apply.getSubmitTime(), 30));

                Long loanNum = requestScope.getLoanNum();

                if (loanNum > 1) {

                    // 这里计算进件时间时，要区分首复贷
                    Borrowing lastBorrowing = borrowingRepository.getLastBorrowing(uid, requestScope.getLoanNum());

                    if (lastBorrowing != null && lastBorrowing.getPayoffTime() != null) {
                        lastSubmitTime = DateUtils.addMinute(lastBorrowing.getPayoffTime(), 30);
                    }

                }

                // 同时缓存包名
                requestScope.setPackageName(apply.getPackageName());

            }

        }

        if (lastSubmitTime == null) {
            // 可能是线上
            lastSubmitTime = new Date();
        }

        // 加入到缓存
        requestScope.setLastSubmitTime(lastSubmitTime);


        return lastSubmitTime;
    }

    @Override
    public Date getLastSubmitTime(String uid, Long loanNum) {

        Date lastSubmitTime = new Date();

        String applyId = requestScope.getApplyId();

        if (!StringUtil.isEmptyOrNvl(applyId)) {

            Optional<Apply> optional = applyRepository.findById(applyId);

            if (optional.isPresent()) {

                Apply apply = optional.get();

                lastSubmitTime = DateUtils.addMinute(apply.getSubmitTime(), 30);

                if (loanNum > 1) {
                    // 这里计算进件时间时，要区分首复贷

                    Borrowing lastBorrowing = borrowingRepository.getLastBorrowing(uid, loanNum);

                    if (lastBorrowing != null && lastBorrowing.getPayoffTime() != null) {
                        lastSubmitTime = DateUtils.addMinute(lastBorrowing.getPayoffTime(), 30);
                    }
                }
            }
        }

        return lastSubmitTime;
    }

    @Override
    public ApplyDTO getApplyInfo(String uid) {

        ApplyDTO dto = new ApplyDTO();

        String applyId = requestScope.getApplyId();

        if (!StringUtil.isEmptyOrNvl(applyId)) {

            Optional<Apply> optional = applyRepository.findById(applyId);

            if (optional.isPresent()) {

                Apply apply = optional.get();

                long createTime = DateUtils.getMillis10(apply.getCreateTime());

                long submitTime = DateUtils.getMillis10(apply.getSubmitTime());

                dto.setApplyAndSubmitDiffTime(submitTime - createTime);

            }


        }

        return dto;
    }

    @Override
    public String getUidByApplyId(String applyId) {

        Optional<Apply> optional = applyRepository.findById(applyId);

        if (optional.isPresent()) {

            Apply apply = optional.get();

            Date lastSubmitTime = DateUtils.addMinute(apply.getSubmitTime(), 30);

            // 临时缓存首贷进件时间：
            requestScope.setFirstLastSubmitTime(DateUtils.addMinute(apply.getSubmitTime(), 30));

            Long loanNum = requestScope.getLoanNum();

            String userId = apply.getUserId();

            if (loanNum > 1) {

                // 这里计算进件时间时，要区分首复贷
                Borrowing lastBorrowing = borrowingRepository.getLastBorrowing(userId, loanNum);

                if (lastBorrowing != null && lastBorrowing.getPayoffTime() != null) {
                    lastSubmitTime = DateUtils.addMinute(lastBorrowing.getPayoffTime(), 30);
                }
            }

            // 同时相关信息
            requestScope.setPackageName(apply.getPackageName());

            requestScope.setLastSubmitTime(lastSubmitTime);

            return userId;

        }

        return null;
    }
}
